package com.itheima.array;

public class BubbleSort {

    public static void main(String[] args) {
        //定义数组
        int[] arr={99,11,33,66,77,55,22,88};
        bubbleSort(arr);
        //排序

      /*  for (int i = 0; i < arr.length - 1-i; i++) {
            if(arr[i]>arr[i+1]){
                int temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }
        for (int i = 0; i < arr.length - 1-i; i++) {
            if(arr[i]>arr[i+1]){
                int temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }*/

        printArray(arr);
    }

    public static void bubbleSort(int[] arr) {
        //外层循环决定次数
        for (int i = 0; i < arr.length-1; i++) {

            //内存循环真正的比较并交换
            for (int j = 0; j < arr.length - 1-i; j++) {
                if(arr[j]>arr[j+1]){
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
    }

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}
